using Domain.Entities;
using Domain.Entities.Enums;
using NHibernate;

namespace Domain.Services
{
    public static class GroupForumPermissionService
    {
        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public static GroupForumPermission GetByGroupIdAndForumId(short groupId, short forumId)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.CreateQuery("from GroupForumPermission gfp where gfp.Group.Id = " + groupId + " and gfp.Forum.Id = " + forumId).UniqueResult<GroupForumPermission>();
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        public static GroupForumPermission Add(Group group, Forum forum, Permission permissions)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                GroupForumPermission groupForumPermission = new GroupForumPermission();
                groupForumPermission.Group = group;
                groupForumPermission.Forum = forum;
                groupForumPermission.Permissions = permissions;
                session.Save(groupForumPermission);

                transaction.Commit();

                return groupForumPermission;
            }
        }

        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public static void Update(GroupForumPermission groupForumPermission, Permission permissions)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                groupForumPermission.Permissions = permissions;
                session.SaveOrUpdate(groupForumPermission);

                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/
        public static void Delete(GroupForumPermission groupForumPermission)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(groupForumPermission);

                transaction.Commit();
            }
        }
    }
}
